package cn.labbook.bluetooth.controller.v2;

import cn.labbook.bluetooth.ammeter.algorithm;
import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.controller.AbstractController;
import cn.labbook.bluetooth.entity.ApplyRecord;
import cn.labbook.bluetooth.service.IApplyRecordService;
import cn.labbook.bluetooth.util.p645.Protocol645;
import cn.labbook.bluetooth.util.p645.Protocol645BuildUtils;
import cn.labbook.bluetooth.util.p645.Protocol645Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: zhangyang
 * @Date: 2023/4/17 10:59
 * @Desc: v2版本 数据处理 controller
 */
@Controller
@RequestMapping("/v2/data/handle")
public class V2DataHandleController extends AbstractController {

    @Autowired
    private IApplyRecordService applyRecordService;


    @RequestMapping("/pay")
    @ResponseBody
    public ResultDto pay(HttpServletResponse response, HttpServletRequest request, String openId, String ammeterId) throws Exception {
        System.out.println("pay");
        HttpSession session = request.getSession();
        String key3 = session.getAttribute("key3").toString();
        String key4 = session.getAttribute("key4").toString();
        ResultDto resultDto = new ResultDto();
        ApplyRecord applyRecord = applyRecordService.getWaitSync(openId, ammeterId);
        if (applyRecord == null) {
            resultDto.setCode(3);
            return resultDto;
        }
        Double payTrueAmount = applyRecord.getPayTrueAmount();
        String s2 = payTrueAmount.toString();
        String[] split = s2.split("\\.");
        String su = split[1];
        if (split[1].length() < 2) {
            for (int i = 0; i < 2 - split[1].length(); i++) {
                su = su + "0";
            }
        }
        su = split[0] + su;
        String price1 = Integer.toHexString(Integer.parseInt(su));
        String count1 = Integer.toHexString(Integer.parseInt(session.getAttribute("count").toString()) + 1);
        session.setAttribute("count", Integer.parseInt(session.getAttribute("count").toString()) + 1);
        String price = price1;
        String count = count1;
        if (price1.length() < 8) {
            for (int i = 0; i < 8 - price1.length(); i++) {
                price = "0".concat(price);
            }
        }
        if (count1.length() < 8) {
            for (int i = 0; i < 8 - count1.length(); i++) {
                count = "0".concat(count);
            }
        }
        String s = price.concat(count);
        byte[] encrypt = algorithm.encrypt(algorithm.toByteArray(s), key3);
        String s1 = "3234343A77665544" + algorithm.encryptResult(encrypt, key4);
        Protocol645 build = Protocol645BuildUtils.build(ammeterId, (byte) 0x03, algorithm.toByteArray(s1));
        byte[] bytes = Protocol645Utils.toBytes(build);
        String s3 = algorithm.toString(bytes);
        Map<String, String> map = new HashMap<>();
        map.put("data", "FEFEFEFE".concat(s3));
        map.put("applyRecordId", applyRecord.getId().toString());

        resultDto.setData(map);
        resultDto.setCode(1);
        return resultDto;
    }

    @RequestMapping("/payReturn")
    @ResponseBody
    public ResultDto payReturn(HttpServletResponse response, HttpServletRequest request, Integer applyRecordId) throws Exception {
        ApplyRecord applyRecord = applyRecordService.getById(applyRecordId);
        if (Objects.isNull(applyRecord)) {
            return fail();
        }

        applyRecord.setSyncStatus(ApplyRecord.SyncStatus.ALREADY.getKey());
        applyRecordService.updateById(applyRecord);
        return success();
    }

}
